//
//  UITextField+JHTextField.m
//  Unity-iPhone
//
//  Created by 陈嘉鸿 on 2019/9/24.
//

#import "UITextField+JHTextField.h"
#import <objc/runtime.h>

static NSString * const JHMaxInputLenghKey = @"maxInputLenghKey";

@implementation UITextField (JHTextField)

#pragma mark - <方法交换>
+ (void)load
{
    Method dealloc = class_getInstanceMethod(self, NSSelectorFromString(@"dealloc"));
    Method jh_dealloc = class_getInstanceMethod(self, @selector(jh_dealloc));
    method_exchangeImplementations(dealloc, jh_dealloc);
    
    Method initWithFrame = class_getInstanceMethod(self, NSSelectorFromString(@"initWithFrame:"));
    Method jh_initWithFrame = class_getInstanceMethod(self, @selector(jh_initWithFrame:));
    method_exchangeImplementations(initWithFrame, jh_initWithFrame);
    
    
    Method initWithCoder = class_getInstanceMethod(self, NSSelectorFromString(@"initWithCoder:"));
    Method jh_initWithCoder = class_getInstanceMethod(self, @selector(jh_initWithCoder:));
    method_exchangeImplementations(initWithCoder, jh_initWithCoder);
}

- (void)jh_dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self jh_dealloc];
}

- (instancetype)jh_initWithFrame:(CGRect)frame
{
    [self jh_initWithFrame:frame];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jh_category_maxInputLengh_textDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:nil];
    return self;
}

- (instancetype)jh_initWithCoder:(NSCoder *)coder
{
    [self jh_initWithCoder:coder];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jh_category_maxInputLengh_textDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:nil];
    return self;
}

#pragma mark - < 属性绑定 >

- (void)setMaxInputLengh:(NSInteger)maxInputLengh
{
    objc_setAssociatedObject(self, (__bridge const void *)(JHMaxInputLenghKey), @(maxInputLengh), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSInteger)maxInputLengh
{
    NSNumber *maxInputLengh = objc_getAssociatedObject(self, (__bridge const void *)(JHMaxInputLenghKey));
    return maxInputLengh.integerValue;
}

#pragma mark - < 输入框内容处理 >

- (void)jh_category_maxInputLengh_textDidChangeNotification:(NSNotification *)notification
{
    UITextField *textField = (UITextField *)notification.object;
    NSString *toBeString = textField.text;
    if (textField.maxInputLengh <= 0)
    {
        //获取高亮部分
        UITextRange *selectedRange = [textField markedTextRange];
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        
        
        if (!position || !selectedRange)
        {
            if ([toBeString containsEmoji])
            {
                //说明包含emoji 过滤
                toBeString = [toBeString removeEmoji];
                textField.text = toBeString;
            }
        }
        return;
    }

    
    //获取高亮部分
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    
    
    if (!position || !selectedRange)
    {
        if ([toBeString containsEmoji])
        {
                //说明包含emoji 过滤
            toBeString = [toBeString removeEmoji];
            textField.text = toBeString;
        }
        
        if (toBeString.length > textField.maxInputLengh)
        {
            textField.text = [toBeString substringToIndex:textField.maxInputLengh];
        }
    }
    
}


@end
